package com.plusjun.seckill.controller;

import com.plusjun.common.utils.R;
import com.plusjun.seckill.service.SeckillService;
import com.plusjun.seckill.to.SeckillSkuRedisTO;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class SeckillController {

    @Resource
    private SeckillService seckillService;

    /**
     * 查询当天的秒杀商品信息
     * @return
     */
    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTO> to = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(to);
    }

    /**
     * 当前商品的秒杀情况
     * @param skuId
     * @return
     */
    @GetMapping("/currentSeckillSkus/{skuId}")
    @ResponseBody

    public R skuSeckillInfo(@PathVariable("skuId")Long skuId){
        SeckillSkuRedisTO redisTO = seckillService.skuSeckillInfo(skuId);
        return R.ok().setData(redisTO);
    }

    /**
     * 秒杀
     * @param killId
     * @param key
     * @param num
     * @return
     */
    @GetMapping("/kill")
    public String secKill(@RequestParam("killId")String killId,
                          @RequestParam("key")String key,
                          @RequestParam("num")Integer num,
                          Model model) {

        String orderSn = seckillService.secKill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

}
